package com.simplecode.arcgistools.form;

import com.esri.arcgisruntime.ArcGISRuntimeEnvironment;
import com.esri.arcgisruntime.geometry.*;
import com.esri.arcgisruntime.mapping.ArcGISMap;
import com.esri.arcgisruntime.mapping.BasemapStyle;
import com.esri.arcgisruntime.mapping.Viewpoint;
import com.esri.arcgisruntime.mapping.view.Graphic;
import com.esri.arcgisruntime.mapping.view.GraphicsOverlay;
import com.esri.arcgisruntime.mapping.view.MapView;
import com.esri.arcgisruntime.symbology.SimpleFillSymbol;
import com.esri.arcgisruntime.symbology.SimpleLineSymbol;
import com.esri.arcgisruntime.symbology.SimpleMarkerSymbol;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.layout.StackPane;
import javafx.scene.paint.Color;
import javafx.stage.Stage;

/**
 * @author liuxy
 * @version 1.0
 * @Project: ArcGis-Tools
 * @DateTime: 2023/5/25 15:14
 * @Description: 主程序入口
 * @since JDK 1.8
 */
public class App extends Application {

    private MapView mapView;

    public static void main(String[] args) {
        Application.launch(args);
    }

    @Override
    public void start(Stage stage) {
        // 设置窗体的标题和大小并展示。 set the title and size of the stage and show it
        stage.setTitle("GIS在线设计工具");
        stage.setWidth(1200);
        stage.setHeight(900);
        stage.show();

        // 创建一个窗体为根节点的JavaFX场景，并将其添加到场景中。create a JavaFX scene with a stack pane as the root node, and add it to the scene
        StackPane stackPane = new StackPane();
        Scene scene = new Scene(stackPane);
        stage.setScene(scene);

        // Note: it is not best practice to store API keys in source code.
        // The API key is referenced here for the convenience of this tutorial.
        // 注意：将 API 密钥存储在源代码中不是最佳做法。为方便本教程，此处引用了 API 密钥。
        String yourApiKey = "--------";
        ArcGISRuntimeEnvironment.setApiKey(yourApiKey);

        // 创建地图视图以显示地图并将其添加到窗体中。 create a map view to display the map and add it to the stack pane
        mapView = new MapView();
        stackPane.getChildren().add(mapView);

        // 地图视图中显示地图数据。Map data is displayed in the map view
        ArcGISMap map = new ArcGISMap(BasemapStyle.ARCGIS_IMAGERY);

        // 在地图视图上设置地图。set the map on the map view
        mapView.setMap(map);

        // 中国 北京 天安门 旗杆 坐标 116.3913092212198,39.905526614768654
        // 在地图上设置视图点，居中到地球上的特定点和比例。Set the viewpoint of the map on the map to a specified point and scale
        // scale 使用 Conversion tool 工具 缩放比例  16级别 9027.977411
        mapView.setViewpoint(new Viewpoint(39.905526614768654,116.3913092212198, 9027.977411));

        // 创建图形叠加并将其添加到地图视图。create a graphics overlay and add it to the map view
        GraphicsOverlay graphicsOverlay = new GraphicsOverlay();
        mapView.getGraphicsOverlays().add(graphicsOverlay);

        // 创建具有位置和空间参考的点几何。create a point geometry with a location and spatial reference
        Point point = new Point( 116.3913092212198,39.905526614768654, SpatialReferences.getWgs84());
        Point point2 = new Point( 115.31873873346568,36.092186505761475, SpatialReferences.getWgs84());

        // 使用不透明的蓝色轮廓符号创建不透明的橙色点符号。create an opaque orange point symbol with a opaque blue outline symbol
        SimpleMarkerSymbol simpleMarkerSymbol =
                new SimpleMarkerSymbol(SimpleMarkerSymbol.Style.CIRCLE, Color.ORANGE, 10);
        SimpleLineSymbol blueOutlineSymbol =
                new SimpleLineSymbol(SimpleLineSymbol.Style.SOLID, Color.BLUE, 2);
        simpleMarkerSymbol.setOutline(blueOutlineSymbol);

        // 使用点几何和符号创建图形。create a graphic with the point geometry and symbol
        Graphic pointGraphic = new Graphic(point, simpleMarkerSymbol);
        Graphic pointGraphic2 = new Graphic(point2, simpleMarkerSymbol);

        // 将点图形添加到图形叠加层。 add the point graphic to the graphics overlay
        graphicsOverlay.getGraphics().add(pointGraphic);
        graphicsOverlay.getGraphics().add(pointGraphic2);

        // 使用空间参考创建点集合，并向其添加三个点。 create a point collection with a spatial reference, and add three points to it
        PointCollection polylinePoints = new PointCollection(SpatialReferences.getWgs84());
        polylinePoints.add(new Point(116.38551672516247,39.90605975290187));
        polylinePoints.add(new Point(116.39131628520181,39.90620071013977));
        polylinePoints.add(new Point(116.39523732979887,39.906327890735206));
        // 从点集合创建折线几何。 create a polyline geometry from the point collection
        Polyline polyline = new Polyline(polylinePoints);

        // 为折线创建不透明的蓝线符号。 create an opaque blue line symbol for the polyline
        SimpleLineSymbol polylineSymbol =
                new SimpleLineSymbol(SimpleLineSymbol.Style.SOLID, Color.BLUE, 3);

        // 使用折线几何图形和符号创建折线图形。 create a polyline graphic with the polyline geometry and symbol
        Graphic polylineGraphic = new Graphic(polyline, polylineSymbol);

        // 将折线图形添加到图形叠加层。 add the polyline graphic to the graphics overlay
        graphicsOverlay.getGraphics().add(polylineGraphic);


        // create a point collection with a spatial reference, and add five points to it
        PointCollection polygonPoints = new PointCollection(SpatialReferences.getWgs84());
        polygonPoints.add(new Point(116.38530078436834,39.921481207172874));//西北
        polygonPoints.add(new Point(116.39574889576066,39.92187289747992));//东北
        polygonPoints.add(new Point(116.39621324046007,39.91191901877684)); //东南
        polygonPoints.add(new Point(116.38582979270309,39.911506457733)); //西南
//        polygonPoints.add(new Point(-118.808558110679, 34.0035027131376));
        // create a polygon geometry from the point collection
        Polygon polygon = new Polygon(polygonPoints);

        // create an orange-red fill symbol with 20% transparency and the opaque blue simple line symbol
        SimpleFillSymbol polygonFillSymbol =
                new SimpleFillSymbol(SimpleFillSymbol.Style.SOLID, Color.web("#FF4500", .8), blueOutlineSymbol);

        // create a polygon graphic from the polygon geometry and symbol
        Graphic polygonGraphic = new Graphic(polygon, polygonFillSymbol);
        // add the polygon graphic to the graphics overlay
        graphicsOverlay.getGraphics().add(polygonGraphic);


    }

    /**
     * 停止并释放应用程序中使用的所有资源。Stops and releases all resources used in application.
     */
    @Override
    public void stop() {
        if (mapView != null) {
            mapView.dispose();
        }
    }

}